﻿using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;

namespace HurricanePackage
{
    /// <summary>
    /// 变换组件助手类
    /// </summary>
    public static class TransformHelper
    {
        /// <summary>
        /// 通过名字寻找子组件
        /// </summary>
        /// <param name="_currentTF">当前对象节点</param>
        /// <param name="_childName">子对象名字</param>
        /// <returns></returns>
        public static Transform FindChildByName([NotNull]this Transform _currentTF, string _childName)
        {
            Transform childTF = _currentTF.Find(_childName);
            if (_childName != null) return childTF;

            for (int i = 0; i < _currentTF.childCount; i++)
            {
                //将任务交给子物体
                childTF = FindChildByName(_currentTF.GetChild(i), _childName);
                if (childTF != null) return childTF;
            }

            return null;
        }
        //示例:  xxtf.FindChildByName("Abutton");

        /// <summary>
        /// 从子物体中查找组件(不包括自身)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_go"></param>
        /// <param name="_includeInactive">是否包含不活跃的子对象</param>
        /// <returns></returns>
        public static T[] GetComponentsInRealChildren<T>(this GameObject _go, bool _includeInactive)
        {
            List<T> TList = new List<T>();
            TList.AddRange(_go.GetComponentsInChildren<T>(_includeInactive));
            TList.RemoveAt(0);
            return TList.ToArray();
        }
        /// <summary>
        /// 从子物体中查找组件(不包括自身)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_tf"></param>
        /// <param name="_includeInactive">是否包含不活跃的子对象</param>
        /// <returns></returns>
        public static T[] GetComponentsInRealChildren<T>(this Transform _tf, bool _includeInactive)
        {
            List<T> TList = new List<T>();
            TList.AddRange(_tf.GetComponentsInChildren<T>(_includeInactive));
            TList.RemoveAt(0);
            return TList.ToArray();
        }
    }
}
